<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
@include_once(D_P.'data/bbscache/o_config.php');
$SCR = 'm_home';
require_once(R_P.'require/element.class.php');// Load Element Class
$element = new Element();
require_once(R_P.'require/showimg.php');
//list($faceurl) = showfacedesign($winddb['icon'],1,'s');


$feeds =  array();
$frienddb = array($winduid);
$query = $db->query("SELECT friendid FROM pw_friends WHERE uid=".pwEscape($winduid)."AND status=0");
while ($rt = $db->fetch_array($query)) {
	$frienddb[] = $rt['friendid'];
}
InitGP('f_type');
if (!empty($frienddb)) {
	$addwhere = '';
	if (in_array($f_type,array('friend','share','photo','write','colony','post','diary'))) {
		$addwhere = ' AND f.type='.pwEscape($f_type);
	}
	if (!$db_dopen) {
		$addwhere .= " AND f.type!='diary'";
	}
	if (!$db_phopen) {
		$addwhere .= " AND f.type!='photo'";
	}
	if (!$o_share_open) {
		$addwhere .= " AND f.type!='share'";
	}
	if (!$o_groups_open) {
		$addwhere .= " AND f.type!='colony'";
	}
	$query = $db->query('SELECT f.*,m.username,m.groupid FROM pw_feed f LEFT JOIN pw_members m ON f.uid=m.uid WHERE f.uid IN('.pwImplode($frienddb).") $addwhere ORDER BY timestamp DESC LIMIT 30");
	while ($rt = $db->fetch_array($query)) {
		$rt['descrip'] = parseFeed($rt['descrip']);
		if ($rt['groupid'] == 6 && $db_shield && $groupid != 3) {
			$rt['descrip'] = appShield('ban_feed');
		}
		$key = get_date($rt['timestamp'],'y-m-d');
		$feeds[$key][] = $rt;
	}
	if (count($frienddb)>10) {
		srand((float) microtime() * 10000000);
		$temp_frienddb = array_rand($frienddb,10);
	} else {
		$temp_frienddb = $frienddb;
	}
	$query = $db->query('SELECT friendid FROM pw_friends WHERE uid IN('.pwImplode($temp_frienddb).') AND status=0 LIMIT 30');
	$mfriends = array();
	while ($rt = $db->fetch_array($query)) {
		if ($rt['friendid'] != $winduid && !in_array($rt['friendid'],$frienddb)) {
			isset($mfriends[$rt['friendid']]) ? $mfriends[$rt['friendid']]++ : $mfriends[$rt['friendid']] = 1;
		}
	}
	arsort($mfriends);
	if (count($mfriends)>6) {
		$i = 0;
		$temp_friend = array();
		foreach ($mfriends as $key => $value) {
			$temp_friend[$key] = $value;
			$i++;
			if ($i==6) {
				break;
			}
		}
		$mfriends = $temp_friend;
		unset($temp_friend);
	}
	$mfriends = array_keys($mfriends);
	if (!$mfriends) {
		@include_once(D_P.'data/bbscache/mode_o_mosort.php');
		if (!$mosort || $mosort['time']<$timestamp-86400) {
			$mosort = array();
			if ($db_ifpwcache & 1) {
				$sql = "SELECT m.uid,m.username,m.icon as face FROM pw_elements e LEFT JOIN pw_members m ON e.id=m.uid WHERE e.type='usersort' AND e.mark='monthpost' ORDER BY e.value DESC ".pwLimit(6);
			} else {
				$montime = PwStrtoTime(get_date($timestamp,'Y-m').'-1');
				$sql = "SELECT m.uid,m.username,m.icon as face FROM pw_memberdata md LEFT JOIN pw_members m USING(uid) WHERE md.lastpost>".pwEscape($montime)." AND md.postnum>0 ORDER BY md.monthpost DESC ".pwLimit(6);
			}
			$query = $db->query($sql);
			while ($rt = $db->fetch_array($query)) {
				list($rt['face'])	= showfacedesign($rt['face'],'show','s');
				$mosort[] = $rt;
			}
			$mosort['time'] = $timestamp;
			writeover(D_P.'data/bbscache/mode_o_mosort.php',"<?php\r\n\$mosort=".pw_var_export($mosort).";\r\n?>");
		}
		unset($mosort['time']);
	} else {
		$query = $db->query('SELECT uid,username,icon as face FROM pw_members WHERE uid IN('.pwImplode($mfriends).')');
		while ($rt = $db->fetch_array($query)) {
			list($rt['face'])	= showfacedesign($rt['face'],'show','s');
			$mosort[] = $rt;
		}
	}
	$mfriends = $mosort;
}
if (!$db_toolbar) {
	$pwForumList = array();
	include_once(D_P.'data/bbscache/forumlist_cache.php');
}
require_once(R_P.'require/header.php');
require_once PrintEot('m_home');
footer();
?>